package color;

import java.awt.Color;

/**
 * This class contain database of all colors
 * 
 */
public class SelectSVGcolors
{
    // array of svg colors
    public String[] svgColor = { "black rgb(0,0,0)", "navy rgb(0,0,128)",
            "darkblue rgb(0,0,139)", "mediumblue rgb(0,0,205)",
            "blue rgb(0,0,255)", "darkgreen rgb(0,100,0)",
            "green rgb(0,128,0)", "teal( rgb(0,128,128)",
            "darkcyan rgb(0,139,139)", "deepskyblue rgb(0,191,255)",
            "darkturquoise rgb(0,206,209)", "mediumspringgreen rgb(0,250,154)",
            "lime rgb(0,255,0)", "springgreen rgb(0,255,127)",
            "cyan rgb(0,255,255)", "aqua rgb(0,255,255)",
            "midnightblue rgb(25,25,112)", "dodgerblue rgb(30,144,255)",
            "lightseagreen rgb(32,178,170)", "forestgreen rgb(34,139,34)",
            "seagreen rgb(46,139,87)", "darkslategray rgb(47,79,79)",
            "darkslategrey rgb(47,79,79)", "limegreen rgb(50,205,50)	",
            "mediumseagreen rgb(60,179,113)", "turquoise rgb(64,224,208)",
            "royalblue rgb(65,105,225)", "steelblue rgb(70,130,180)",
            "darkslateblue rgb(72,61,139)", "mediumturquoise rgb(72,209,204)",
            "indigo rgb(75,0,130)	", "darkolivegreen rgb(85, 107, 47)",
            "cadetblue rgb(95,158,160)", "cornflowerblue rgb(100,149,237)",
            "orchid rgb(218,112,214)", "mediumaquamarine rgb(102,205,170)",
            "dimgrey rgb(105,105,105)", "dimgray rgb(105, 105, 105)",
            "slateblue rgb(106,90,205)", "olivedrab rgb(107,142,35)",
            "slategrey rgb(112,128,144)", "slategray rgb(112,128,144)",
            "lightslategray rgb(119,136,153)",
            "lightslategrey rgb(119,136,153)",
            "mediumslateblue rgb(123,104,238)", "lawngreen rgb(124,252,0)",
            "chartreuse rgb(127,255,0)", "white rgb(255, 255, 255)",
            "aquamarine rgb(127,255,212)", "yellowgreen rgb(128,0,0)",
            "purple rgb(128,0,128)", "olive rgb(128,128,0)",
            "gray rgb(128,128,128)", "grey rgb(128, 128, 128)",
            "skyblue rgb(135,206,235)", "lightskyblue rgb(135,206,250)",
            "blueviolet rgb(138,43,226)", "darkred rgb(139,0,0)",
            "darkmagenta rgb(139,0,139)", "saddlebrown rgb(139,69,19)",
            "darkseagreen rgb(143,188,143)", "lightgreen rgb(144,238,144)",
            "mediumpurple rgb(147, 112, 219)", "darkviolet rgb(148,0,211)",
            "palegreen rgb(152,251,152)", "darkorchid rgb(153,50,204)",
            "yellowgreen rgb(154,205,50)", "sienna rgb(160,82,45)",
            "brown rgb(165,42,42)", "darkgray rgb(169,169,169)",
            "darkgrey rgb(169,169,169)", "lightblue rgb(173,216,230)",
            "greenyellow rgb(173, 255, 47)", "paleturquoise rgb(175,238,238)",
            "lightsteelblue rgb(176,196,222)", "powderblue rgb(176,224,230)",
            "firebrick rgb(178,34,34)", "darkgoldenrod rgb(184,134,11)",
            "mediumorchid rgb(186,85,211)", "rosybrown rgb(188,143,143)",
            "darkkhaki rgb(189,183,107)", "silver rgb(192,192,192)",
            "mediumvioletred rgb(199,21,133)", "indianred rgb(205,92,92)",
            "peru rgb(205,133,63)", "chocolate rgb(210,105,30)",
            "tan rgb(210,180,140)", "lightgray rgb(211,211,211)",
            "lightgrey rgb(211,211,211)", "thistle rgb(216,191,216)",
            "goldenrod rgb(218,165,32)", "palevioletred rgb(219,112,147)",
            "crimson rgb(220,20,60)", "gainsboro rgb(220,220,220)",
            "plum rgb(221,160,221)", "burlywood rgb(222,184,135)",
            "lightcyan rgb(224,255,255)", "lavender rgb(230,230,250)",
            "darksalmon rgb(233,150,122)", "violet rgb(238,130,238)",
            "palegoldenrod rgb(238,232,170)", "lightcoral rgb(240,128,128)",
            "khaki rgb(240,230,140)", "aliceblue rgb(240,248,255)",
            "honeydew rgb(240,255,240)", "azure rgb(240,255,255)",
            "sandybrown rgb(244,164,96)", "wheat rgb(245,222,179)",
            "beige rgb(245,245,220)", "whitesmoke rgb(245,245,245)",
            "mintcream rgb(245,255,250)", "ghostwhite rgb(248,248,255)",
            "salmon rgb(250,128,114)", "antiquewhite rgb(250,235,215)",
            "linen rgb(250,240,230)", "lightgoldenrodyellow rgb(250,250,210)",
            "oldlace rgb(253,245,230)", "red rgb(255,0,0)",
            "fuchsia rgb(255,0,255)", "magenta rgb(255,0,255)",
            "deeppink rgb(255,20,147)", "orangered rgb(255,69,0)",
            "tomato rgb(255,99,71)", "hotpink rgb(255,105,180)",
            "coral rgb(255,127,80)", "darkorange rgb(255,140,0)",
            "lightsalmon rgb(255,160,122)", "orange rgb(255,165,0)",
            "lightpink rgb(255,182,193)", "pink rgb(255,192,203)",
            "gold rgb(255,215,0)", "peachpuff rgb(255,218,185)",
            "navajowhite rgb(255,222,173)", "moccasin rgb(255,228,181)",
            "bisque rgb(255,228,196)", "mistyrose rgb(255,228,225)",
            "blanchedalmond rgb(255, 235, 205)", "papayawhip rgb(255,239,213)",
            "seashell rgb(255,245,238)", "cornsilk rgb(255,248,220)",
            "lemonchiffon rgb(255,250,205)", "floralwhite rgb(255,250,240)",
            "snow rgb(255,250,250)", "yellow rgb(255,255,0)",
            "lightyellow rgb(255,255,224)", "ivory rgb(255,255,240)",
            "lavenderblush rgb(255,240,245)", };
    
    /**
     * This method will take color code string and convert to real color
     * 
     * @param colorname
     *            takes Color as a string
     * @return colorname color name as a string
     */
    public String getRgbByName(String colorname)
    {
        for (int i = 0; i < svgColor.length; i++)
        {
            int a = svgColor[i].indexOf("(");
            String subString = svgColor[i].substring(0, a - 4);
            if (svgColor[i].contains(colorname)
                    && colorname.length() == subString.length()) return svgColor[i];
        }
        return colorname;
    }
    
    /**
     * decode rgb color in format rgb(r%,r%,r%)
     * 
     * @param stroke
     *            take color name as a string
     * @return gives intended matching java color
     */
    public Color getRGB1(String stroke)
    {
        int a = stroke.indexOf("(");
        int b = stroke.indexOf(")");
        String rgb = new String(stroke.substring(a + 1, b));
        int c = rgb.indexOf(",");
        String gb = new String(rgb.substring(c + 1, rgb.length()));
        int c2 = gb.indexOf(",");
        String blue = new String(gb.substring(c2 + 1, gb.length() - 1));
        int k = parseInt(rgb.substring(0, c - 1));
        int k2 = parseInt(rgb.substring(0, c - 1));
        int k3 = parseInt(blue);
        double redpercent = k * 255 / 100;
        double greenpercent = k2 * 255 / 100;
        double bluepercent = k3 * 255 / 100;
        rangecheck(redpercent);// check for 0-255 range
        rangecheck(greenpercent);
        rangecheck(bluepercent);
        return new Color((int) redpercent, (int) greenpercent,
                (int) bluepercent);
    }
    
    /**
     * Get color by RGB
     * 
     * @param stroke
     *            take color name as a string
     * @return gives intended matching java color
     */
    
    public Color getRGB2(String stroke)
    {
        int a = stroke.indexOf("(");
        int b = stroke.indexOf(")");
        String rgb = new String(stroke.substring(a + 1, b));
        int c = rgb.indexOf(",");
        String r = new String(rgb.substring(0, c));
        String gb = new String(rgb.substring(c + 1, rgb.length()));
        int c2 = gb.indexOf(",");
        String r2 = new String(gb.substring(0, c2));
        String blue = new String(gb.substring(c2 + 1, gb.length()));
        return new Color(parseInt(rgb.substring(0, c)), parseInt(gb.substring(
                0, c2)), parseInt(blue));
        
    }
    
    /**
     * rgb color in format rgb(rrr,rrr,rrr)
     * 
     * @param stroke
     *            take color name as a string
     * @return gives intended matching java color
     */
    
    public Color getRGB3(String stroke)
    {
        
        String sgr = getRgbByName(stroke);
        
        if (sgr == stroke)
        {
            System.out.println("Error Color not Suported by passer... " + sgr);
            System.exit(0);
        }
        int a = sgr.indexOf("(");
        
        int b = sgr.indexOf(")");
        String rgb2 = new String(sgr.substring(a + 1, b));
        int c = rgb2.indexOf(",");
        String r = new String(rgb2.substring(0, c));
        String gb = new String(rgb2.substring(c + 1, rgb2.length()));
        int c2 = gb.indexOf(",");
        String r2 = new String(gb.substring(0, c2));
        String blue = new String(gb.substring(c2 + 1, gb.length()));
        return new Color(parseInt(rgb2.substring(0, c)), parseInt(gb.substring(
                0, c2)), parseInt(blue));
        
    }
    
    /**
     * extention of java parseInt function to handle errors
     * 
     * @param string
     *            rgb value as a string
     * @return value range 0-255
     */
    
    public int parseInt(String string)
    {
        
        try
        {
            int kk = Integer.parseInt(string);
            if (0 > kk || kk > 255)
            {
                System.out.println("Error SVG Color out of range---"
                        + string.toString());
                System.exit(0);
                
            }
            return kk;
        }
        catch (Exception e)
        {
            
            System.out.println("Error in SVG file units Exiting---"
                    + string.toString());
            System.exit(0);
            
        }
        return (Integer) null;// return null
        
    }
    
    /**
     * function to handle java standard color range
     * 
     * @param value
     *            take rgb value
     * @return value if range 0-255
     */
    public double rangecheck(double value)
    {
        if (0 > value || value > 255)
        {
            System.out.println("Error... SVG Color out of range---");
            System.exit(0);
        }
        return value;
        
    }
}
